package com.ocom.nettyxf;


import com.ocom.common.utils.GmUtil;
import com.ocom.common.utils.MD5;
import com.ocom.common.utils.NumConvertUtil;
import com.ocom.nettyxf.util.SM2Util;
import com.ocom.nettyxf.util.sm2.SM2KeyHelper;
import com.ocom.nettyxf.util.sm2.SM2KeyPair;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.nio.charset.Charset;

@SpringBootTest
public class Sm2TestSign {

    public static final Charset charset = Charset.forName("utf-8");
    private static final String ID = "31323334353637383132333435363738";
    private static final String input = "3333353633343839303934413035303643323337413439363533393544363142";


    @Test
    public  void test() throws Exception {
        String region ="1234";
        String regionMd5 = MD5.MD5Encode(region,"gbk");
        System.out.println(regionMd5);

        SM2KeyPair sm2KeyPair = new SM2KeyPair(
                NumConvertUtil.hexStringToByte("4BECFA44FE4C9819F784B7DF7216288A6DB33F947C10B3413BAF3A12330F99DB"),
                NumConvertUtil.hexStringToByte("EAC5FC31FD67D30224FF8F8918B00936F5392E1BB6C3653C69272A7111AF5B70"),
                NumConvertUtil.hexStringToByte("EBAB7A0EEC89A627CF21595EAC64295307CC28CF02F3B284C8707A0638F4D609")
        );

        ECPublicKeyParameters ecPublicKeyParameters = SM2KeyHelper.buildECPublicKeyParameters(sm2KeyPair);
        ECPrivateKeyParameters ecPrivateKeyParameters = SM2KeyHelper.buildECPrivateKeyParameters(sm2KeyPair.getPrivateKey());

//        SM2SignResult signRet = SM2Helper.sign(NumConvertUtil.hexStringToByte(input), ecPrivateKeyParameters, null);
//        System.out.println("input==="+ input);
//        System.out.println("signResultR_mergeRS:"+Hex.toHexString(signRet.mergeRS()));
//        System.out.println("signResultR_withID:"+Hex.toHexString(signRet.getSignR()));
//        System.out.println("signResultS_withID:"+Hex.toHexString(signRet.getSignS()));
//        boolean verifySignRet = SM2Helper.verifySign(input.getBytes(charset), signRet, ecPublicKeyParameters, null);
//        System.out.println("======签名校验2===="+verifySignRet);

        String r33=SM2Util.sign("EBAB7A0EEC89A627CF21595EAC64295307CC28CF02F3B284C8707A0638F4D609",
                input);
        System.out.println("======签名校验2===="+r33);

        byte[]  yhtuj = GmUtil.rsAsn1ToPlainByteArray(NumConvertUtil.hexStringToByte("304502202de040885b2fd32a0f36f723e700ab9ce08f6efec88f1c6d503a80d8f7597f9302210096cf4324022fcf96e28ebc1ec2031a7fb5183520907e7ec56447335438c28689"));
        System.out.println(NumConvertUtil.bytesToHexString(yhtuj));




    }


    @Test
    public  void test9987() throws Exception {
        byte[]  yhtuj = GmUtil.rsAsn1ToPlainByteArray(NumConvertUtil.hexStringToByte("2de040885b2fd32a0f36f723e700ab9ce08f6efec88f1c6d503a80d8f7597f9396cf4324022fcf96e28ebc1ec2031a7fb5183520907e7ec56447335438c28689"));
        System.out.println(NumConvertUtil.bytesToHexString(yhtuj));
    }





}
